
% function nh = flattenHistory(history, howMany)
% 
% given the history in the form of little histories of trajectories, combines all
% of them into a single flat history (each little history contains some
% trajectory, and here we put all these trajectories back to back)
% -howMany (optional): how many mini histories to include

function nh = flattenHistory(history, howMany)
    if nargin ==1   %we want all of the data
        nh = history{1}; 

        s = length(nh) + 1; 
        for i=2: length(history)
           for j=1: length(history{i})
              nh{s} = history{i}{j}; 
              s = s+1; 
           end
        end
    else    % we should randomly select the starting index of mini histories

        %this version selects each individual index randomly, so the
        %flattened history could be from different places 
%         inds = floor( rand(1,howMany)*length(history) + 1); 
%         nh = history{inds(1)};
%         s = length(nh)+1; 
%         for i=2: howMany
%            for j=1: length(history{inds(i)})
%                nh{s} = history{inds(i)}{j}; 
%                s = s+1;  
%            end
%         end


        %this version selects only the starting point of the history
        %randomly. it then selects the rest consecutively to make sure the
        %returned history is not chopped. 
        startInd = floor(rand* (length(history)-howMany+1)+1); 
        nh = history{startInd}; 
        s = length(nh)+1; 
        for i=2: howMany
            for j=1: length(history{startInd+i-1}) 
                nh{s} = history{startInd+i-1}{j}; 
                s= s+1; 
            end
        end

    end
end